package com.cardbaobao.cardbabyclient.ui.fragment.base;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.cardbaobao.cardbabyclient.base.App;
import com.cardbaobao.cardbabyclient.controller.Controller;
import com.cardbaobao.cardbabyclient.ui.dialog.LoadingDialog;
import com.cardbaobao.cardbabyclient.utils.ActivityUtil;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;


/**
 * BaseFragment
 * Created by zad
 * 2016/8/1 9:11
 */

public abstract class BaseFragment extends Fragment {
    protected long mEnterPageTime;
    protected String mCurrentPageId;

    protected Context mContext;

    private View mContentView;

    private LoadingDialog mLoadingDialog;

    protected Intent mIntent = null;

    protected App mApplication;

    protected Context mApplicationContext;

    public Controller mController;
    /**
     * Activity名称
     */
    private String mActivityName;

    public static <T extends Fragment> T newInstance(Class clazz, Bundle args) {
        T mFragment = null;
        try {
            mFragment = (T) clazz.newInstance();
        } catch (java.lang.InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        mFragment.setArguments(args);
        return mFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        mApplication = ApplicationHelper.getApplication();
        mApplicationContext= ApplicationHelper.getApplicationContext();
        mController = new Controller(mContext);
        initData(savedInstanceState);
    }

    protected abstract int getLayoutId();

    protected abstract void initViewFinish(View view, Bundle savedInstanceState);

    protected void initData(Bundle savedInstanceState) {
    }

    protected void initIntent() {
        if (mIntent == null) {
            mIntent = new Intent();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(mContentView == null){
            mContentView = inflater.inflate(getLayoutId(), container, false);
        }
        return mContentView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ButterKnife.bind(this,mContentView);
        initViewFinish(mContentView, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(mContentView != null){
            ((ViewGroup)mContentView.getParent()).removeView(mContentView);
        }
    }

    /**
     * 显示加载动画
     *
     * @param isTouchHide 是否触摸屏幕 动画消失
     */
    public void showProgressDialog(boolean isTouchHide) {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(mContext);
        }
        mLoadingDialog.setCanceledOnTouchOutside(isTouchHide);
        if (!isTouchHide) {
            mLoadingDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    ((Activity) mContext).finish();
                    return true;
                }
            });
        }
        mLoadingDialog.setShowText(null);
        if(!mLoadingDialog.isShowing()){
            mLoadingDialog.show();
        }
    }

    /**
     *  显示加载动画
     *
     * @param isTouchHide 是否触摸屏幕 动画消失
     * @param showText    加载中的提示语
     */
    protected void showProgressDialog(boolean isTouchHide, String showText) {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(mContext);
        }
        mLoadingDialog.setShowText(showText);
        mLoadingDialog.setCanceledOnTouchOutside(isTouchHide);
        if (!isTouchHide) {
            mLoadingDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    mLoadingDialog.dismiss();
                    ((Activity) mContext).finish();
                    return true;
                }
            });
        }
        if(isVisible()&&!mLoadingDialog.isShowing()){
            mLoadingDialog.show();
        }
    }

    /**
     * 隐藏加载动画
     */
    public void dismissProgressDialog() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }

    /**
     * 隐藏LinearLayout Divider
     *
     * @param linearLayout
     */
    protected void hideDivider(LinearLayout linearLayout) {
        if (linearLayout != null) {
            linearLayout.setDividerDrawable(null);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if(mActivityName == null){
            mActivityName = ActivityUtil.getRunningActivityName(mContext);
        }
        MobclickAgent.onPageStart(mActivityName);//友盟统计
    }

    @Override
    public void onPause() {
        super.onPause();
        mEnterPageTime=System.currentTimeMillis();
        if(mActivityName == null){
            mActivityName = ActivityUtil.getRunningActivityName(mContext);
        }
        MobclickAgent.onPageEnd(mActivityName);//友盟统计
    }

}
